package com.medical.doctor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.common.utils.R;
import com.medical.doctor.controller.vo.WallentVo;
import com.medical.doctor.dao.DoctorMessageDao;
import com.medical.doctor.entity.DoctorMessageEntity;
import com.medical.doctor.service.DoctorMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.Query;

import com.medical.doctor.dao.DoctorWalletDao;
import com.medical.doctor.entity.DoctorWalletEntity;
import com.medical.doctor.service.DoctorWalletService;


@Service("doctorWalletService")
public class DoctorWalletServiceImpl extends ServiceImpl<DoctorWalletDao, DoctorWalletEntity> implements DoctorWalletService {
    @Autowired
    private DoctorWalletDao walletDao;
    @Autowired
    private DoctorMessageService doctorMessageService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DoctorWalletEntity> page = this.page(
                new Query<DoctorWalletEntity>().getPage(params),
                new QueryWrapper<DoctorWalletEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R findById(WallentVo vo) {
        Page<DoctorWalletEntity> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        IPage<DoctorWalletEntity> walletEntityIPage= walletDao.findById(page,vo);
        PageUtils pageUtils = new PageUtils(walletEntityIPage);
        //计算收入与支出
        BigDecimal pay= BigDecimal.valueOf(0.00);
        BigDecimal in= BigDecimal.valueOf(0.00);
        List<DoctorWalletEntity> walletEntities = walletEntityIPage.getRecords();
        for (DoctorWalletEntity walletEntity : walletEntities) {
            if (walletEntity.getOrderType().equals(1)){
                BigDecimal pays = pay.add(walletEntity.getOrderMoney());
                pay=pays;
            }else {
                BigDecimal ins = in.add(walletEntity.getOrderMoney());
                in=ins;
            }
        }
        //获得医生的金额
        DoctorMessageEntity doctorMessageEntity = doctorMessageService.getById(vo.getDoctorId());
        return R.ok().put("data",pageUtils).put("pay",pay).put("in",in).put("money",doctorMessageEntity.getMoney());
    }

}